
import { Graph } from '@antv/x6'
import { Dnd } from '@antv/x6-plugin-dnd'
import React from 'react'
import { Button } from 'antd';


// 控制面板
const ControlPanel= () => {
  return (
    <div className='w-[200px] p-2'>
      控制面板
    </div>
  )
}

// 物料堆
const list = [
  {
    title: '长方形',
    value: 'rect'
  },
  {
    title: '圆形',
    value: 'circle'
  },
]


// 操作面板
export default class FlowChaat extends React.Component {
  private graph!: Graph
  private container!: HTMLDivElement
  private dndContainer!: HTMLDivElement;
  private dnd!: Dnd;
  private cell: any

  componentDidMount() {
    const graph = new Graph({
      container: this.container,
      background: {
        color: '#F2F7FA',
      },
      mousewheel: {
        enabled: true,
        modifiers: ['ctrl', 'meta'],
      },
    })

    const source = graph.addNode({
      x: 130,
      y: 30,
      width: 100,
      height: 40,
      label: 'Hello',
      attrs: {
        body: {
          stroke: '#8f8f8f',
          strokeWidth: 1,
          fill: '#fff',
          rx: 6,
          ry: 6,
        },
      },
    })

    const target = graph.addNode({
      x: 180,
      y: 160,
      width: 100,
      height: 40,
      label: 'World',
      attrs: {
        body: {
          stroke: '#8f8f8f',
          strokeWidth: 1,
          fill: '#fff',
          rx: 6,
          ry: 6,
        },
      },
    })

    graph.addEdge({
      source,
      target,
      attrs: {
        line: {
          stroke: '#8f8f8f',
          strokeWidth: 1,
          strokeDasharray: 5,
        },
      },
    })
    graph.centerContent()

    this.dnd = new Dnd({
      target: graph,
      scaled: false,
      dndContainer: this.dndContainer,
    })

    
    // graph.addEdge({
    //   source: { x: 540, y: 40 },
    //   target: { x: 580, y: 180 },
    //   vertices: [{ x: 540, y: 140 }],
    //   connector: { name: 'smooth' },
    //   attrs: {
    //     line: {
    //       stroke: '#1890ff',
    //       strokeDasharray: 5,
    //       targetMarker: 'classic',
    //       style: {
    //         animation: 'ant-line 30s infinite linear',
    //       },
    //     },
    //   },
    // })

    // 单击节点事件： e, x, y, node, view 
    graph.on('node:click', ({ node }) => {
      this.cell = node
    })

    this.graph = graph
  }

  startDrag = (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => {
    const target = e.currentTarget
    const type = target.getAttribute('data-type')
    const node =
      type === 'rect'
        ? this.graph.createNode({
            width: 100,
            height: 40,
            label: 'Rect',
            attrs: {
              body: {
                stroke: '#8f8f8f',
                strokeWidth: 1,
                fill: '#fff',
                rx: 6,
                ry: 6,
              },
            },
          })
        : this.graph.createNode({
            width: 60,
            height: 60,
            shape: 'circle',
            label: 'Circle',
            attrs: {
              body: {
                stroke: '#8f8f8f',
                strokeWidth: 1,
                fill: '#fff',
              },
            },
          })

    this.dnd.start(node, e.nativeEvent as any)
  }

  refContainer = (container: HTMLDivElement) => {
    this.container = container
  }

  dndContainerRef = (container: HTMLDivElement) => {
    this.dndContainer = container
  }

  render() {
    return (
      <div className="dnd-app border h-screen flex">
        {/* 物料堆 */}
        <div className="dnd-wrap w-[200px] flex gap-4 p-2" ref={this.dndContainerRef}>
          {
            list.map((item) => {
              return (
                <Button
                  key={item.value}
                  data-type={item.value}
                  className={`dnd-${item.value}`}
                  onMouseDown={this.startDrag}
                >{item.title}</Button>
              )
            })
          }
        </div>

        {/* 操作面板 */}
        <div className="app-content flex-1" ref={this.refContainer} />

        <ControlPanel />
      </div>
    )
  }
}
